package com.dylan.历史.java7;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Created by Dylan on 2018/7/30.
 */
public class 路径 {

    public static void main(String[] args) {
        // 绝对路径
        Path absolute = Paths.get("/", "Users", "ankang");
        // 相对路径
        Path relative = Paths.get("../../", "test", "goods_123.sql");
        System.out.println(absolute.toFile().exists());
        System.out.println(relative.toFile().exists());

        // p.resolve(q),如果q是绝对路径则返回q,否则返回"p然后q"
        Path projectPath = Paths.get("/Users/ankang/workspace/dylan-review-snippet");
        Path path1 = projectPath.resolve(relative);
        System.out.println(path1.toFile().getPath()); // /Users/ankang/workspace/dylan-review-snippet/../../test/goods_123.sql

        // 获取兄弟路径
        Path path2 = projectPath.resolveSibling("baybox_job");
        System.out.println(path2.toFile().getPath()); // /Users/ankang/workspace/baybox_job

        // 去掉冗余目录
        Path path3 = path1.normalize();
        System.out.println(path3.toFile().getPath()); // /Users/ankang/test/goods_123.sql

        // 返回绝对路径
        Path path4 = relative.toAbsolutePath();
        System.out.println(path4.toFile().getPath()); // /Users/ankang/workspace/dylan-review-snippet/../../test/goods_123.sql

        // 获取父目录
        Path path5 = relative.getParent();
        System.out.println(path5.toFile().getPath()); // ../../test

        // 获取文件名
        Path path6 = relative.getFileName();
        System.out.println(path6.toFile().getPath()); // goods_123.sql
    }
}
